const wxapi = require("../../../utils/wxapi");
import { Util } from "../../../utils/util";
const CryptoJS = require('../../../utils/crypto-js');
import { API_SNZO_FILE_URL, API_SNZO_URL } from "../../../utils/wxapi";

// pages/center/record/record.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curTab: 0,
    userInfo: null,
    balanceOfData: [],
    billOfData: [],
    balanceTotal: 0,
    billTotal: 0,
    scrollTop: 0,
    canScroll: true,
    balancePageParams: { currPage: 1, pageSize: 15 },
    billPageParams: { pageNo: 1, pageSize: 20 },
    isTriggered: false
  },

  /**
   * 标签页点击切换
   * @param {*} e 
   */
  changeTab: function (e) {
    let curTab = e.currentTarget.dataset.curtab;
    this.setData({
      curTab
    })
    if (curTab == 0) {
      wx.setNavigationBarTitle({
        title: '卡余额详情'
      })
      if (this.data.balanceOfData.length == 0) {
        this.getBalanceBill();
      }
    } else if (curTab == 1) {
      wx.setNavigationBarTitle({
        title: '账户详情'
      })
      if (this.data.billOfData.length == 0) {
        this.getRecord();
      }
    }
  },

  /**
   * 标签页滑动切换
   */
  swiperChange: function (e) {
    if (e.detail.source == 'touch') {
      let curTab = e.detail.current;
      this.setData({
        curTab
      })
      if (curTab == 0) {
        wx.setNavigationBarTitle({
          title: '卡余额详情'
        })
        if (this.data.balanceOfData.length == 0) {
          this.getBalanceBill();
        }
      } else if (curTab == 1) {
        wx.setNavigationBarTitle({
          title: '账户详情'
        })
        if (this.data.billOfData.length == 0) {
          this.getRecord();
        }
      }
    }
  },

  /**
   * 获取余额详情
   */
  getBalanceBill() {
    wx.showLoading({
      title: '加载中...',
    })
    const allParams = {
      ...this.data.balancePageParams,
      cardNo: app.globalData.userInfo.cardNo,
      timestamp: new Date().getTime(),
    }
    const trimParams = Util.trimNulls(allParams);
    const pendSign = { ...trimParams, url: '/transaction/queryV2' };
    const sign = Util.jsonSort(pendSign);
    const md5Sign = CryptoJS.MD5(sign);
    const params = { sign: md5Sign, ...trimParams };
    const headers = {
      'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
    };
    wxapi.queryUserTransaction(params, headers).then(res => {
      wx.hideLoading()
      this.setData({
        isTriggered: false
      })
      if (res.code === 20000) {
        if (this.data.balancePageParams.currPage === 1) {
          this.data.balanceOfData = [];
          this.setData({
            scrollTop: 0
          })
        }
        const listOfData = res.data.content;
        const newData = this.data.balanceOfData.concat(listOfData);
        this.setData({
          balanceOfData: newData,
          balanceTotal: res.data.totalSize,
          canScroll: true
        })
      } else {
        wx.showModal({
          title: '提示信息',
          content: res.message,
          showCancel: false
        })
      }
    })
  },

  /**
   * 获取账户详情
   */
  getRecord() {
    wx.showLoading({
      title: '加载中...',
    })
    const bodyParams = {
      ...this.data.billPageParams,
      stk: app.globalData.userInfo.cardNo,
      orderType: 0,
      startTime: null,
      endTime: null
    }
    wxapi.getWalletLogList(bodyParams).then(res => {
      wx.hideLoading()
      this.setData({
        isTriggered: false
      })
      if (res.result === '1') {
        if (this.data.billPageParams.pageNo === 1) {
          this.data.billOfData = [];
          this.setData({
            scrollTop: 0
          })
        }
        const listOfData = res.object;
        const newData = this.data.billOfData.concat(listOfData);
        this.setData({
          billOfData: newData,
          billTotal: res.page.totalRecord,
          canScroll: true
        })
      } else {
        wx.showModal({
          title: '提示信息',
          content: res.messageInfo,
          showCancel: false
        })
      }
    })
  },

  /**
   * 上拉加载更多
   */
  scrollToLower() {
    if (this.data.curTab == 0) {
      if (this.data.balanceOfData.length === this.data.balanceTotal) {
        return;
      }
      this.setData({
        canScroll: false
      })
      this.data.balancePageParams.currPage++;
      this.getBalanceBill();
    } else if (this.data.curTab == 1) {
      if (this.data.billOfData.length === this.data.billTotal) {
        return;
      }
      this.setData({
        canScroll: false
      })
      this.data.billPageParams.pageNo++;
      this.getRecord()
    }
  },

  /**
   * 下拉刷新
   */
  refresherToLoad() {
    if (this.data.curTab == 0) {
      this.data.balancePageParams.currPage = 1;
      this.getBalanceBill();
    } else if (this.data.curTab == 1) {
      this.data.billPageParams.pageNo = 1;
      this.getRecord();
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userInfo: app.globalData.userInfo
    })
    this.getBalanceBill();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.refresherToLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})